﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Duckbill
{
    class MapState : BaseState
    {
        const float viewSpeed = 5000f;
        const float zoomSpeed = 0.4f;

        KeyboardState oldKeys = Keyboard.GetState();
        Vector2 view = Vector2.Zero;
        float zoom = 1f;

        public MapState()
        {
            Player p = Game.player;
            view = p.pos - Game.SCREEN/2;
            view.X += p.room.X*Game.SCREEN_WIDTH;
            view.Y += p.room.Y*Game.SCREEN_HEIGHT;
        }

        public override void Update(float time)
        {
            KeyboardState keys = Keyboard.GetState();
            if (keys.IsKeyDown(Keys.Enter) && !oldKeys.IsKeyDown(Keys.Enter))
                Game.states.Pop();
            if (keys.IsKeyDown(Keys.Right))
                view.X += viewSpeed*time;
            if (keys.IsKeyDown(Keys.Left))
                view.X -= viewSpeed*time;
            if (keys.IsKeyDown(Keys.Down))
                view.Y += viewSpeed*time;
            if (keys.IsKeyDown(Keys.Up))
                view.Y -= viewSpeed*time;
            if (keys.IsKeyDown(Keys.OemPlus))
                zoom /= 1-time*zoomSpeed;
            if (keys.IsKeyDown(Keys.OemMinus))
                zoom *= 1-time*zoomSpeed;

            oldKeys = keys;
        }
        public override void Draw(SpriteBatch batch)
        {
            Sprite.scale /= 12/zoom;
            foreach (Room r in Game.map.rooms)
            {
                Sprite.offset = new Vector2((r.X+6)*Game.SCREEN_WIDTH,
                    (r.Y+6)*Game.SCREEN_HEIGHT) - view;
                Vector2 c = r.camera;
                r.camera = Vector2.Zero;
                if (r == Game.player.room)
                    Game.player.Draw(batch);
                r.Draw(batch);
                r.camera = c;
            }
            Sprite.scale *= 12/zoom;
            Sprite.offset = Vector2.Zero;
        }
    }
}
